package com.tech.koufu.ui.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.tech.koufu.R;
import com.tech.koufu.model.StockInfo;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.tools.KouFuTools;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter; 
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 首页列表
 * @author 123
 *
 */
public class MainStocksAdapter extends BaseAdapter {
	public ArrayList<UserStocks> stockinfo;
	private Context context;  
	private LayoutInflater mInflater = null; 
	public MainStocksAdapter(Context context){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public MainStocksAdapter(Context context , ArrayList<UserStocks> stockinfo) {
		super();
		this.stockinfo = stockinfo;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() { 
		return stockinfo.size();
	}

	@Override
	public Object getItem(int position) { 
		return stockinfo.get(position);
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_stock, parent,false); 
             holder.txt_stock_name = (TextView)convertView.findViewById(R.id.txt_stock_name);  
             holder.btn_charge = (TextView)convertView.findViewById(R.id.btn_charge);  
             holder.lv_item_curprice_gains = (TextView)convertView.findViewById(R.id.lv_item_curprice_gains);
             holder.lv_item_cost_value = (TextView)convertView.findViewById(R.id.lv_item_cost_value);
             holder.lv_item_stock_num_value = (TextView)convertView.findViewById(R.id.lv_item_stock_num_value);
             holder.lv_item_breakevenamount_price = (TextView)convertView.findViewById(R.id.lv_item_breakevenamount_price);
             holder.lv_item_stock_market_price = (TextView)convertView.findViewById(R.id.lv_item_stock_market_price);
             
             holder.btn_lv_tog = (ImageView)convertView.findViewById(R.id.btn_lv_tog);
             holder.ly_lv_tog = (LinearLayout)convertView.findViewById(R.id.ly_lv_tog);
             holder.ly_item_stock_num = (RelativeLayout)convertView.findViewById(R.id.ly_item_stock_num);
             holder.ly_item_stock_market = (RelativeLayout)convertView.findViewById(R.id.ly_item_stock_market);
             
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         final View ly_item_stock_num = holder.ly_item_stock_num;
         final View ly_item_stock_market = holder.ly_item_stock_market;
         final ImageView btn_lv_tog = holder.btn_lv_tog;
         holder.ly_lv_tog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(ly_item_stock_num.getVisibility() == View.VISIBLE){
					ly_item_stock_num.setVisibility(View.GONE);
					ly_item_stock_market.setVisibility(View.GONE);
					btn_lv_tog.setImageResource(R.drawable.btn_lv_open);
				}else{
					ly_item_stock_num.setVisibility(View.VISIBLE);
					ly_item_stock_market.setVisibility(View.VISIBLE);
					btn_lv_tog.setImageResource(R.drawable.btn_lv_close);
				}
			}
		});
         UserStocks info = (UserStocks)getItem(position);
         DecimalFormat decimalFormat=new DecimalFormat("##0.00");
//         Log.w("Tt", info.getStockdqj()+">>"+info.getStockHold()+">"+info.getStockbj()+">");
         String dqj = info.getStockdqj()!=null?info.getStockdqj():"0";
         Float breakevenamount_price = Float.parseFloat(dqj) * Float.parseFloat(info.getStockHold()) - 
        		 Float.parseFloat(info.getStockbj()) * Float.parseFloat(info.getStockHold());
         Float charge = Float.parseFloat(dqj) * Float.parseFloat(info.getStockHold()) / 
        		 Float.parseFloat(info.getAllmoney()) * 100;
         Float stock_market_price = Float.parseFloat(dqj) * Float.parseFloat(info.getStockHold());
         holder.txt_stock_name.setText(info.getStockName());
         holder.lv_item_breakevenamount_price.setText("" + decimalFormat.format(breakevenamount_price));
         holder.lv_item_breakevenamount_price.setTextColor(
        		 context.getResources().getColor(KouFuTools.getStockValueColor(breakevenamount_price+""))
        		 );
         holder.lv_item_curprice_gains.setText(dqj);
         holder.btn_charge.setText("" + decimalFormat.format(charge) + "%仓");
         holder.lv_item_cost_value.setText(info.getStockbj());
         holder.lv_item_stock_num_value.setText(info.getStockHold());
         holder.lv_item_stock_market_price.setText(""+ decimalFormat.format(stock_market_price));
		return convertView;
	}
	static class ViewHolder  
    {   
		public ImageView btn_lv_tog;
        public TextView txt_stock_name;  
        public TextView btn_charge;
        public TextView lv_item_curprice_gains;
        public TextView lv_item_cost_value;
        public TextView lv_item_stock_num_value;
        public TextView lv_item_breakevenamount_price;
        public TextView lv_item_stock_market_price;
        public LinearLayout ly_lv_tog;
        public RelativeLayout ly_item_stock_num,ly_item_stock_market;
    }  
	
}   
